package com.controller;

import com.domain.Blog;
import com.domain.Gallery;
import com.domain.GalleryImg;
import com.service.impl.BlogServiceImpl;
import com.service.impl.GalleryServiceImpl;
import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class GalleryController {

    @Autowired
    private BlogServiceImpl blogService;
    @Autowired
    private GalleryServiceImpl galleryService;

    //前台显示gallery数据
    @GetMapping("/gallery")
    public String gallery(Model model){
        List<Blog> hotBlogs = blogService.findHotBlog();
        model.addAttribute("hotBlogs",hotBlogs);
        //获取置顶gallery数据
        List<Gallery> gallerys = galleryService.findTopGallery();
        model.addAttribute("gallerys",gallerys);
        return "gallery";
    }

    //跳转gallerymanage页面
    @GetMapping("/gallerymanage")
    public String gallerymanage(){
        return "redirect:galleryMsg";
    }

    //后台显示gallery数据
    @GetMapping("/galleryMsg")
    public ModelAndView showGallery(HttpSession session,String gid){
        ModelAndView modelAndView = new ModelAndView("template/gallerymanage");
        //判断session中是否有数据
        List<Gallery> galleryList = (List<Gallery>) session.getAttribute("galleryList");
        if (galleryList == null || galleryList.size() == 0){
            //获取gallery数据
            galleryList = galleryService.findAllGallery();
            session.setAttribute("galleryList",galleryList);
        }
        //gid不存在时，即第一次打开或跳转，显示第一个gallery的数据
        if (gid == null || gid == "") {
            gid = "1";
        }
        //存储当前选中的gallery的信息
        int g_id = Integer.parseInt(gid);
        for (Gallery gallery : galleryList) {
            if (gallery.getId() == g_id){
                modelAndView.addObject("galleryOne",gallery);
                continue;
            }
        }
        //获取对应gallery的img数据
        List<GalleryImg> galleryImgList = galleryService.findGalleryImg(g_id);
        modelAndView.addObject("galleryImgList",galleryImgList);
        return modelAndView;
    }

    //删除gallery中的img
    @GetMapping("/deleteGalImg")
    public String deleteGalleryImg(HttpSession session, String id,String gid){
        galleryService.deleteGalleryImg(Integer.parseInt(id));
        return "forward:galleryMsg";
    }

    //修改gallery描述信息
    @PostMapping("/updateGalMsg")
    @ResponseBody
    public int updateGalleryMsg(HttpSession session, String galid, String galmsg){
        Gallery gallery = new Gallery();
        gallery.setId(Integer.parseInt(galid));
        gallery.setGmsg(galmsg);
        int i = galleryService.updateGalleryMsg(gallery);
        //获取gallery数据
        List<Gallery> galleryList = galleryService.findAllGallery();
        session.setAttribute("galleryList",galleryList);
        return i;
    }

    //删除gallery(判断是否还有图片，有，不删除)
    @GetMapping("/deleteGal")
    public String deleteGallery(HttpSession session,String gid) throws IOException {
        int g_id = Integer.parseInt(gid);
        List<GalleryImg> galleryImgList = galleryService.findGalleryImg(g_id);
        if(galleryImgList.size() != 0){
            return "redirect:galleryMsg?gid="+gid;
        }
        galleryService.deleteGallery(g_id);
        session.removeAttribute("galleryList");
        return "redirect:galleryMsg";
    }

    //上传图片
    @PostMapping("/addGalImg")
    @ResponseBody
    public String uploadGalImg(HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file,@RequestParam(value = "id", required = false) String id){
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        //获取文件名及后缀
        String fileName = file.getOriginalFilename();
        //重新生成文件名
        fileName = UUID.randomUUID().toString().replace("-","") + "_" + fileName;
        //指定上传文件地址
        String dirPath = "D:\\IDEA_Project\\blog-springboot\\target\\classes\\static\\img\\";
        File filePath = new File(dirPath);
        if (!filePath.exists()){
            filePath.mkdirs();
        }
        try {
            file.transferTo(new File(dirPath+fileName));
            GalleryImg galleryImg = new GalleryImg();
            galleryImg.setGimg("img/"+fileName);
            galleryImg.setGid(Integer.parseInt(id));
            galleryService.addGalImg(galleryImg);
            return "true";
        } catch (IOException e) {
            e.printStackTrace();
            return "redirect:galleryMsg";
        }
    }

    //新建gallery
    @PostMapping("/addGallery")
    @ResponseBody
    public int addGallery(HttpSession session,String galtitle,String galmsg){
        Gallery gallery = new Gallery();
        gallery.setGmsg(galmsg);
        gallery.setGtitle(galtitle);
        gallery.setCreatetime(" " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int i = galleryService.addGallery(gallery);
        session.removeAttribute("galleryList");
        return i;
    }

    //获取对应gallery中的img数据
//    @GetMapping("/findGalimg")
//    @ResponseBody
//    public List<GalleryImg> getGalleryImg(String gid){
//        List<GalleryImg> galleryImgList = galleryService.findGalleryImg(Integer.parseInt(gid));
//        return galleryImgList;
//    }
}
